<?php
class AccountController extends Forum_Controller_Action
{
    public function indexAction()
    {
        if (Model_User::getUserId() === false) {
            $this->_redirect('account/login');
        }
        
        $user = Forum::getSingleton('user')->find(Model_User::getUserId());
        
        $thread_count = Forum::registry('db')->fetchOne(
            "SELECT COUNT(id) 
             FROM forum_thread
             WHERE user_id = {$user['id']}"
        );
        
        $message_count = Forum::registry('db')->fetchOne(
            "SELECT COUNT(id) 
             FROM forum_message
             WHERE user_id = {$user['id']}"
        );
        
        $this->view->username        = $user['username'];
        $this->view->date_registered = $user['registered_on'];
        $this->view->date_visited    = $user['last_login_on'];
        $this->view->thread_count    = $thread_count ? $thread_count : 0;
        $this->view->message_count   = $message_count ? $message_count : 0;
        
        $this->render();
    }
    
    public function loginAction()
    {
        if (Model_User::getUserId() !== false) {
            $this->_redirect('account');
        }
        
        $data = $this->_getParam('user');
        
        if (count($data)) {
            if (Forum::getSingleton('user')->validateLogin($data)) {
                $row = Forum::getSingleton('user')->getUserByEmailOrName($data['username']);
                $date = date('Y-m-d H:i:s');
                Forum::getSingleton('user')->update($row['id'], array("last_login_on" => $date), true);
                $_SESSION['user_id'] = $row['id'];
                $_SESSION['is_admin'] = false;
                if ($row['is_admin'] == 1) {
                    $_SESSION['is_admin'] = true;
                }
                if (isset($_SERVER['HTTP_REFERER'])) {
                    $this->_redirect($_SERVER['HTTP_REFERER'], false);
                } else {
                    $this->_redirect('account');
                }
            }
            $this->view->username = $data['username'];
            $this->view->password = $data['password'];
        }
        
        $this->render();
    }
    
    public function logoutAction()
    {
        $this->layout->disableLayout();
        
        if (Model_User::getUserId() === false) {
            if (isset($_SERVER['HTTP_REFERER'])) {
                $this->_redirect($_SERVER['HTTP_REFERER'], false);
            } else {
                $this->_redirect();
            }
        }
        
        unset($_SESSION['user_id']);
        if (isset($_SESSION['is_admin'])) {
            unset($_SESSION['is_admin']);
        }
        
        if (isset($_SERVER['HTTP_REFERER'])) {
            $this->_redirect($_SERVER['HTTP_REFERER'], false);
        } else {
            $this->_redirect();
        }
    }
    
    public function createAction()
    {
        $data = $this->_getParam('register');
        
        if (count($data)) {
            if (Forum::getSingleton('user')->save($data)) {
                return $this->_redirect('index');
            }
            $this->view->username = $data['username'];
            $this->view->email = $data['email'];
        }
        
        $this->render();
    }
    
    public function activateAction()
    {
        if (Model_User::getUserId() !== false) {
            $this->_redirect('account');
        }
        
        $data = $this->_getAllParams();
        
        if (isset($data['username']) && isset($data['code'])) {
            if (Forum::getSingleton('user')->activate($data)) {
                return $this->_redirect('account/login');
            }
            $this->view->username = $data['username'];
        }
        
        $this->render();
    }
    
    public function sendActivationAction()
    {
        $this->layout->disableLayout();
        
        $email = $this->_getParam('activation-email');
        
        if (empty($email)) {
            Forum_Message::getInstance()->addError("Вкажіть електронну адресу вашого профілю");
            return $this->_redirect('account/activate');
        }
        
        Forum::getSingleton('user')->sendActivationCode($email, 'register');
        
        return $this->_redirect('account/activate');
    }
    
    public function passwordAction()
    {
        $data = $this->_getParam('password');
        
        if (count($data)) {
            if (Forum::getSingleton('user')->validatePasswordChange($data)) {
                Forum::getSingleton('user')->update(Model_User::getUserId(), array(
                    'password' => md5($data['new'])
                ), true);
                Forum_Message::getInstance()->addSuccess('Ваш пароль успішно змінено');
                $this->_redirect('account');
            }
            
            $this->view->password_old = $data['old'];
            $this->view->password_new = $data['new'];
            $this->view->password_new_confirmation = $data['new_confirmation'];
        }
        
        $this->render();
    }
    
}